package com.hzit.loan.repay.controller;

import cn.hutool.core.lang.Snowflake;
import com.hzit.loan.repay.iclent.IPayWebClient;
import com.hzit.payweb.api.enums.PayStatusEnum;
import com.hzit.payweb.api.enums.PayWayEnum;
import com.hzit.payweb.api.req.PayReq;
import com.hzit.payweb.api.resp.RespVo;
import com.hzit.payweb.api.resp.data.PayRespData;
import com.hzit.payweb.api.utils.PayDigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 还款接口
 */
@RestController
@RequestMapping("/repay")
public class RepayController {

    @Autowired
    private IPayWebClient payWebClient;

    /**
     * 返回支付方式列表
     * @return
     */
    public String queryPayWay(){
        //调用支付服务的接口

        return null;
    }

    /**
     * 发起支付
     */
    @RequestMapping("/sendPay")
    public RespVo<PayRespData> sendPay(){


        //先。调用token服务 TODO

        String token = "";


        String reqKey = "test1234";

        //1、获取接口所需参数
        String mchId = "1000";
        String bank = "Alipay";
        String payWay = PayWayEnum.ALIPAY_WAP_PAY.getCode();

        Snowflake snowflake = new Snowflake(10,8);
        String orderId = snowflake.nextIdStr(); // 雪花算法。分布式系统中如何生成唯一id

        String orderAmt = "100.00";

        //2.封装参数
        PayReq payReq = new PayReq();

        payReq.setBank(bank);
        payReq.setBody("测试商品");
        payReq.setMchId(mchId);
        payReq.setPayWay(payWay);
        payReq.setOrderAmt(orderAmt);
        payReq.setOrderId(orderId);
        payReq.setSubjetc("test");

        //3.按照接口文档生成签名值

        String sign = null;
        try {
            sign = PayDigestUtil.getSign(payReq,reqKey);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        payReq.setSign(sign);


        RespVo<PayRespData> respVo = payWebClient.pay(payReq);
        System.out.println("--------respVo--"+respVo.getData().getPayUrl());

        //解析返回的结果

        return respVo;

    }

}
